<template>
  <div :class="{'indexTable bgWhite customer expandTable': true, 'tableExpand': !isExpand}" ref="content" id="afterSale">
   <el-form ref="form" class="pb_form ">
      <el-select  v-model="searchForm.issue.value" placeholder="Issue" clearable  @keyup.enter.native="getTableList" @focus="getNameEnList" >
        <el-option v-for="item in issueList" :key="item.id" :value="item.value" :label="item.label"></el-option>
      </el-select>
      <el-select  v-model="searchForm.country.value" placeholder="Country" clearable  @keyup.enter.native="getTableList" @focus="getNameEnList" >
        <el-option v-for="item in nameEnList" :key="item.id" :value="item.value" :label="item.label"></el-option>
      </el-select>
     <el-select  v-model="searchForm.member_type.value" placeholder="订阅类型" clearable  @keyup.enter.native="getTableList" @focus="getNameEnList" >
        <el-option v-for="item in memberTypeList" :key="item.id" :value="item.value" :label="item.label"></el-option>
      </el-select>
      <el-input v-model="searchForm.name.value " placeholder="Name" @keyup.enter.native="getTableList"></el-input>
      <el-input v-model="searchForm.email.value " placeholder="Email" @keyup.enter.native="getTableList"></el-input>
      
      <el-select  v-model="searchForm.purchased_item.value" placeholder="Purchased Item" clearable  @keyup.enter.native="getTableList" @focus="getNameEnList" >
        <el-option v-for="item in purchaseItemList" :key="item.id" :value="item.value" :label="item.label"></el-option>
      </el-select>
       <el-select  v-model="searchForm.is_buy_thirty_day.value" placeholder="Purchased within 30 days？" clearable  @keyup.enter.native="getTableList" @focus="getNameEnList" >
        <el-option v-for="item in yesNOList" :key="item.id" :value="item.value" :label="item.label"></el-option>
      </el-select>
      <el-input v-model="searchForm.order_no.value " placeholder="Order ID" @keyup.enter.native="getTableList"></el-input>
      <el-input v-model="searchForm.sn_code.value " placeholder="S/N Code" @keyup.enter.native="getTableList"></el-input>
      <el-input v-model="searchForm.case_id.value " placeholder="Case ID" @keyup.enter.native="getTableList"></el-input>
      <el-select  v-model="searchForm.where_did_you_know_bonoch_from.value" placeholder="Where did you know bonoch from?" clearable  @keyup.enter.native="getTableList" @focus="getNameEnList" >
        <el-option v-for="item in knowBonochList" :key="item.id" :value="item.value" :label="item.label"></el-option>
      </el-select>
       <el-select  v-model="searchForm.which_product_are_you_interesed.value" placeholder="Which Product are you interested in?" clearable  @keyup.enter.native="getTableList" @focus="getNameEnList" >
        <el-option v-for="item in interestedProductList" :key="item.id" :value="item.value" :label="item.label"></el-option>
      </el-select>
       <el-select  v-model="searchForm.would_you_like_to_subscribe_to_bonoch.value" placeholder="Would you like to subscribe to bonoch?" clearable  @keyup.enter.native="getTableList" @focus="getNameEnList" >
        <el-option v-for="item in yesNOList" :key="item.id" :value="item.value" :label="item.label"></el-option>
      </el-select>

     
      

      <div class="createTime">
         <el-input v-model="localTime" placeholder="当地创建时间"></el-input>
         <el-date-picker @change="changeLocalTime"   v-model="localTimePicker"
          type="datetimerange" range-separator="至"  :default-time="['00:00:00' , '23:59:59']" 
         start-placeholder="开始日期"  value-format="yyyy-MM-dd HH:mm:ss" end-placeholder="结束日期" placeholder="选择操作时间"></el-date-picker>
      </div>
       <div class="createTime">
         <el-input v-model="updateTime" placeholder="创建时间"></el-input>
         <el-date-picker @change="changeUpdateTime"   v-model="updateTimePicker"
          type="datetimerange" range-separator="至"  :default-time="['00:00:00' , '23:59:59']" 
         start-placeholder="开始日期"  value-format="yyyy-MM-dd HH:mm:ss" end-placeholder="结束日期" placeholder="选择操作时间"></el-date-picker>
      </div>
      <div class="isExpand" @click="isExpandForm">
        <p class="hide" v-show="isExpand">收起 <i class="iconfont icon-arrow_1"></i></p>
        <p class="expand" v-show="!isExpand">展开 <i class="iconfont icon-arrow_1"></i></p>
      </div>
    </el-form>
    <div class="btnContainer" ref="btn">
      <div>
        <el-button type="del" @click="sureCloseDialog('deal')" v-if="$_has('/after_sale/set_status')">处理完成</el-button>
        <el-button type="del" @click="closeExportDialog()" v-if="$_has('/after_sale/export')">导出</el-button>
      </div>
      <div>
        <el-button type="search" @click="getTableList" v-if="$_has('/after_sale/list')">查询</el-button>
        <el-button type="del" @click="resetForm">重置</el-button>
      </div>
    </div>
    <el-table :data="listTable" :height="tableH" :row-class-name="rowClassName" ref="table" @row-click="RowClick"
     @selection-change="handleSelectionChange" :row-key="function(row){return row.id}" v-loading="loading">
      <el-table-column type="selection" width="64"  fixed="left" :reserve-selection="true"></el-table-column>
      <el-table-column  label="Issue" width="296" prop="issue" align="left"></el-table-column>
      <el-table-column  label="Country"  width="200"   prop="country" ></el-table-column>
      <el-table-column  label="Name" width="224" prop="name" align="left"></el-table-column>
      <el-table-column  label="Email"  width="224"   prop="email" align="left"></el-table-column>
      <el-table-column  label="订阅类型"  width="224"   prop="member_type" align="left"></el-table-column>
      <el-table-column  label="Purchased Item"  width="304" prop="purchased_item" ></el-table-column>
      <el-table-column  label="Purchased within 30 days？" width="240" prop="is_buy_thirty_day" ></el-table-column>
      <el-table-column  label="Order ID"  width="200"   prop="order_no"></el-table-column>
      <el-table-column  label="订单号是否有效" width="200"  prop="order_is_valid"> </el-table-column>

      <el-table-column  label="S/N Code" width="200" prop="sn_code" ></el-table-column>
      <el-table-column  label="Case ID" width="200" prop="case_id" ></el-table-column>
      <el-table-column  label="Where did you know bonoch from?" width="300"  prop="where_did_you_know_bonoch_from"> </el-table-column>
      <el-table-column  label="Which Product are you interestedin?" width="300"  prop="which_product_are_you_interesed"> </el-table-column>
      <el-table-column  label="Would you like to subscribe to bonoch?" width="320"  prop="would_you_like_to_subscribe_to_bonoch"> </el-table-column>
      <el-table-column  label="Description"  width="400" prop="description" align="left"></el-table-column>

      <el-table-column  label="Image" width="292" prop="image" align="left" >
        <template slot-scope="scope" @click.stop>
          <div  class="comment_image">
            <el-image v-for="item in scope.row.httpImage" :key="item"  :preview-src-list="utils.setCdnUrl() + scope.row.httpImage"
            :src="utils.setCdnUrl() +item"  ></el-image>
          </div>
        
        </template>
      </el-table-column>
      <el-table-column  label="当地创建时间"  width="200" prop="local_time" ></el-table-column>
      <el-table-column  label="创建时间" width="200" prop="create_time" ></el-table-column>
    </el-table>
    <el-pagination ref="pagination" background :page-sizes="pageSizes" :total="total"  :page-size="pageSize"
    layout="total, sizes, prev, pager, next, jumper"  @size-change="handleSizeChange" :current-page="page"
      @current-change="handleCurrentChange"></el-pagination>
    <FinishDeal v-show="checkShow" :idList="curList" :show="checkShow" @closeDialog="closeCheckDialog"></FinishDeal>
    <Export v-show="exportShow" :show="exportShow" @closeDialog="closeExportDialog"></Export>
  </div>
</template>

<script>
import { Export, FinishDeal } from './components'
import { afterSaleList } from '@/api/customer/afterSale.js'
import { setSearchParams } from '@/utils/index.js'
import {  nameEnList } from '@/api/manage/site.js'

export default {
  name: 'after_sale',
  data() {
    return {
      searchForm: {
        issue: { value: '', op: '=' },
        country: { value: '',op: '=' },
        member_type: { value: '',op: 'like' },
        name: { value: '',op: 'like' },
        email: { value: '',op: 'like' },
        purchased_item: { value: '', op: '=' },
        sn_code: { value: '', op: 'like' },
        case_id: {value: '', op: 'like'},
        is_buy_thirty_day: {value: '', op: '='},
        where_did_you_know_bonoch_from: {value: '', op: 'like'},
        which_product_are_you_interesed:{ value: '', op: '=' },
        would_you_like_to_subscribe_to_bonoch: { value: '', op: '=' },
        order_no: { value: '', op: 'like' },

      },
      yesNOList: [
        {value: 'Yes', label: 'Yes'},
        {value: 'No', label: 'No'},
      ],
 

      issueList: [
        {value: 'My item is defective.', label: 'My item is defective.'},
        {value: 'I am interested in bonoch. I need more information.', label: 'I am interested in bonoch. I need more information.'},
        {value: 'I just want to get in touch.', label: 'I just want to get in touch.'},
      ],
      memberTypeList: [
        {value: '订阅', label: '订阅'},
        {value: '注册', label: '注册'},
        {value: 'Warranty', label: 'Warranty'},
        {value: 'Tester', label: 'Tester'},
      ],
      nameEnList: [] ,// 国家英文名下拉框
      purchaseItemList: [
        {value: 'Baby monitor', label: 'Baby monitor'},
        {value: 'Video doorbell ', label: 'Video doorbell '},
        {value: 'Smart curtain robot', label: 'Smart curtain robot'},
      ],

      knowBonochList:  [
        {value: 'Amazon', label: 'Amazon'},
        {value: 'Family or friends', label: 'Family or friends'},
        {value: 'Meida', label: 'Meida'},
        {value: 'Influencer', label: 'Influencer'},
        {value: 'bonoch social media', label: 'bonoch social media'},
        {value: 'Kickstarter/Indiegogo', label: 'Kickstarter/Indiegogo'},
        {value: 'Other', label: 'Other'},
      ],
      interestedProductList: [
        {value: 'Baby monitor', label: 'Baby monitor'},
        {value: 'Video doorbell', label: 'Video doorbell'},
        {value: 'Smart curtain robot', label: 'Smart curtain robot'},
        {value: 'Just bonoch', label: 'Just bonoch'},
      ],
      pageSizes: [20, 50, 100],
      total: 10,
      pageSize: 20,
      page: 1,
      exportShow: false,
      checkShow: false,
      curList: [],
      nameCnList: [] ,// 国家种文名下拉框
      curId: null,
      tableH: null,
      curIdList: [],
      selectList: [],
      loading: false,
      isExpand: false,
      localTimePicker: '',
      localTime: '',
      updateTimePicker: '',
      updateTime: ''
    }
  },
  watch:{
    localTimePicker:function(n){
      if(n==null){
        this.localTime = ""
      }
    },
    updateTimePicker:function(n){
      if(n==null){
        this.updateTime = ""
      }
    }
  },
  created(){
    this.getTableList()
  },
  mounted(){
    let that = this;
    that.setTableHeight()
    // 注：window.onresize只能在项目内触发1次
    window.onresize = function windowResize () {
        // 通过捕获系统的onresize事件触发我们需要执行的事件
      that.setTableHeight()
    }
  },
  methods: {
    changeLocalTime (arr) {
      if(arr){
        let str = arr[0] + ' - ' + arr[1]
        this.localTime = arr[0].split(' ')[0] +' - '+arr[1].split(' ')[0]
        this.searchForm.local_time.value = str
      }else{
        this.searchForm.local_time.value = null
      }
      this.getTableList()
    },
    changeUpdateTime (arr) {
      if(arr){
        let str = arr[0] + ' - ' + arr[1]
        this.updateTime = arr[0].split(' ')[0] +' - '+arr[1].split(' ')[0]
        this.searchForm.create_time.value = str
      }else{
        this.searchForm.create_time.value = null
      }
      this.getTableList()
    },
    isExpandForm () {
      this.isExpand = !this.isExpand
      if(this.isExpand ) {
        this.tableH = parseInt(this.tableH) - 91 + 'px'
      }else {
        this.tableH = parseInt(this.tableH) + 91 + 'px'
      }

    },
    RowClick (row) {
      this.$refs.table.toggleRowSelection(row);
    } ,
    setTableHeight(){
      let searchFormH = this.$refs.form.$el.offsetHeight;
      let manageMenuH = this.$refs.content.offsetHeight;
      let btnContainerH = this.$refs.btn.offsetHeight;
      let paginationH = this.$refs.pagination.$el.offsetHeight
      let tableHeight = manageMenuH  - btnContainerH - searchFormH - paginationH - 48 - 24 - 24
      this.tableH = tableHeight +'px' 
    },
    sureCloseDialog(type){
      if(this.curList.length > 0){
         if( type == 'deal'){
          this.closeCheckDialog()
        }
       
      } else {
        let word = "处理完成"
        this.$message.error('请选择需' + word + '的选项！')
      }
      
    },
    getTableList(){
      this.loading = true
      let params = setSearchParams(this.searchForm, this.pageSize, this.page)
      afterSaleList(params).then(res=>{
        let list = res.data.data
        if( list.length == 0 && this.page > 1){
          this.page -= 1
          this.getTableList()
        }
        let idList = []
        for(let i = 0; i < list.length; i++){
          if(list[i].status == 1) {
            list[i].statusBoolean = true
          } else {
            list[i].statusBoolean = false
          }
          if(list[i].image){
            list[i].httpImage = list[i].image.split(',')
          }
          
          idList.push(list[i].id)
        }
        this.curIdList = idList
        this.listTable = list
        this.total = res.data.total
        this.page = res.data.current_page
        this.$refs.table.clearSelection()
        this.loading = false
      })
    },
    handleSizeChange(val) {
      this.pageSize = val
      this.getTableList()
     
    },
    handleCurrentChange(val) {
      this.page = val
      this.getTableList()
      this.$refs.table.clearSelection()
    },
    setCurRow(row, type) {
      this.curList = []
      this.curList.push(row.id)
      this.curId = row.id

      if(type == 'check'){ 
        this.closeCheckDialog()  
      }else if(type == 'subscribe'){
        this.closeSubscribeDialog()
      }
      
    },
    closeCheckDialog () {
      this.checkShow = !this.checkShow
      if(!this.checkShow){
        this.getTableList()
      }
    },
    closeSubscribeDialog(){
      this.subscribeShow = !this.subscribeShow
      if(!this.subscribeShow){
        this.getTableList()
      }
    },
    closeExportDialog(){
      this.exportShow = !this.exportShow
      if(!this.exportShow){
        this.getTableList()
      }
    },
    rowClassName({row}){
      let rowClass = "tableRowClass"
       if(row.statusSelection){
        rowClass += ' selectRow'
      }
      return rowClass
    },
    handleSelectionChange(list){
      let array = []
      for(let i = 0; i < list.length; i++ ){
        array.push(list[i].id)
      }
      this.curList = array

      // 选中行的样式赋值
      for(let i in this.selectList){
        this.selectList[i].statusSelection = false
      }
      for (let item in list){
        list[item].statusSelection = true
      }
      this.selectList = list
    },
     // 重置按钮
    resetForm(){
      for(let item in this.searchForm){
        this.searchForm[item].value =""
      }
      this.localTime = ""
      this.updateTime = ''
      this.updateTimePicker =''
      this.localTimePicker = ''
      this.getTableList()
    },
    getNameEnList(){ // 国家英文名称
      nameEnList().then(res=>{
        this.nameEnList = res.data
      })
    },

  },
  components: { Export, FinishDeal }

}
</script>

<style lang="scss">
@import '@/styles/customer/aftersale.scss';



</style>